package com.platform.modules.sys.service.impl;


import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.enumstatus.AptitudeEnum;
import com.platform.common.utils.Query;
import com.platform.modules.sys.dao.SysOrgAptitudeDao;
import com.platform.modules.sys.entity.SysAptitudeExtEntity;
import com.platform.modules.sys.entity.SysExamineStatusEntity;
import com.platform.modules.sys.entity.SysOrgAptitudeEntity;
import com.platform.modules.sys.entity.SysOrgUserEntity;
import com.platform.modules.sys.service.SysAptitudeExtService;
import com.platform.modules.sys.service.SysExamineStatusService;
import com.platform.modules.sys.service.SysOrgAptitudeService;

/**
 * @author wangLc
 * 注册机构人员信息  
 * 2019-5-18
 */
@Service("SysOrgAptitudeService")
public class SysOrgAptitudeServiceImpl extends ServiceImpl<SysOrgAptitudeDao, SysOrgAptitudeEntity> implements SysOrgAptitudeService {
	@Autowired
	private SysAptitudeExtService sysAptitudeExtService;
	@Autowired
	private SysExamineStatusService sysExamineStatusService;
	@Autowired
	private SysOrgAptitudeService sysOrgAptitudeService;

	@Override
	public List<SysOrgAptitudeEntity> queryAll(Map<String, Object> params) {
		return baseMapper.selectListPage(params);
	}
	
	
	@Override
	public Page queryAllPage(Map<String, Object> params) {
		Page<SysOrgAptitudeEntity> page = new Query<SysOrgAptitudeEntity>(params).getPage();
		List<SysOrgAptitudeEntity> orgAptitudeList = baseMapper.selectListPage(page,params);
		
		return  page.setRecords(orgAptitudeList);
	}
	
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteBatch(String[] userIds) {
		this.removeByIds(Arrays.asList(userIds));
	}
	
	@Override
	public Page queryPage(Map<String, Object> params) {
		// 排序
		params.put("sidx", "T.CREATE_TIME");
		params.put("asc", false);
		Page<SysOrgAptitudeEntity> page = new Query<SysOrgAptitudeEntity>(params).getPage();
		
		List<SysOrgAptitudeEntity> orgAptitudeList = baseMapper.selectListPage(page, params);
		
		if(orgAptitudeList != null && orgAptitudeList.size()>0){
			for(SysOrgAptitudeEntity orgAptitude:orgAptitudeList){
				//证书有效截止日期
				Date expiryEndTime = orgAptitude.getExpiryEndTime();
				if(expiryEndTime != null){
					//当前时间
					Date now = new Date();
					Integer a = expiryEndTime.compareTo(now);
					//如果证书有效截止日期小于当前时间则证明证书已经过期
					if(a<0 && !"3".equals(orgAptitude.getAptitudeStatus())){
						orgAptitude.setAptitudeStatus("4"); //状态置为失效
						baseMapper.updateById(orgAptitude);
					}
				}

				Map<String, Object> orgAptitudeParams = new HashMap<String, Object>();
				orgAptitudeParams.put("aptitudeId", orgAptitude.getId());
				List<SysAptitudeExtEntity> aptitudeExtList = sysAptitudeExtService.queryAll(orgAptitudeParams);
				orgAptitude.setAptitudeExtList(aptitudeExtList);
			}
		}
		
		return page.setRecords(orgAptitudeList);
	}
	
	
	
	
	
	
	
	
	@Override
	public List<SysOrgAptitudeEntity> queryList(Map<String, Object> params) {
		// 排序
		params.put("sidx", "T.CREATE_TIME");
		params.put("asc", false);
	
		List<SysOrgAptitudeEntity> orgAptitudeList = baseMapper.selectListPage(params);
		
		if(orgAptitudeList != null && orgAptitudeList.size()>0){
			for(SysOrgAptitudeEntity orgAptitude:orgAptitudeList){
				//证书有效截止日期
				Date expiryEndTime = orgAptitude.getExpiryEndTime();
				if(expiryEndTime != null){
					//当前时间
					Date now = new Date();
					Integer a = expiryEndTime.compareTo(now);
					//如果证书有效截止日期小于当前时间则证明证书已经过期
					if(a<0 && !"3".equals(orgAptitude.getAptitudeStatus())){
						orgAptitude.setAptitudeStatus("4"); //状态置为失效
						baseMapper.updateById(orgAptitude);
					}
				}

				Map<String, Object> orgAptitudeParams = new HashMap<String, Object>();
				orgAptitudeParams.put("aptitudeId", orgAptitude.getId());
				List<SysAptitudeExtEntity> aptitudeExtList = sysAptitudeExtService.queryAll(orgAptitudeParams);
				orgAptitude.setAptitudeExtList(aptitudeExtList);
			}
		}
		
		return orgAptitudeList;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void add(SysOrgAptitudeEntity sysOrgAptitudeEntity) {
		this.save(sysOrgAptitudeEntity);
	}
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public String passOrgAptitude(SysOrgAptitudeEntity sysOrgAptitudeEntity,String auditTxt,String auditor,String orgNo) {
		
		//审核状态
		 SysExamineStatusEntity examineStatus = new SysExamineStatusEntity();
 		Map<String, Object> params = new HashMap<String, Object>();
 		params.put("auditId", sysOrgAptitudeEntity.getId());
// 		params.put("auditOrgNo", orgNo);
 		params.put("auditType", AptitudeEnum.aptitudeType.APTITUDE.getKey());
 		List<SysExamineStatusEntity> list = sysExamineStatusService.queryAll(params);
	 	if(list != null && list.size()>0){
	 		    examineStatus.setId(list.get(0).getId()+"");
		    		examineStatus.setAuditTxt(auditTxt);
		    		examineStatus.setAuditor(auditor);
		    		examineStatus.setAuditOrgNo(orgNo);
		    		examineStatus.setAuditStatus(AptitudeEnum.aptitudeStatus.YBSYPZ.getKey());
	 		    sysExamineStatusService.updateById(examineStatus);
	 	}else{
	 		  	examineStatus.setAuditTxt(auditTxt);
	 		  	examineStatus.setAuditor(auditor);
	 		  	examineStatus.setAuditOrgNo(orgNo);
	 		  	examineStatus.setAuditTime(new Date());
	 		  	examineStatus.setAuditId(sysOrgAptitudeEntity.getId());
	 		  	examineStatus.setAuditType(AptitudeEnum.aptitudeType.APTITUDE.getKey());
	 		  	examineStatus.setAuditStatus(AptitudeEnum.aptitudeStatus.YBSYPZ.getKey());
	 	        sysExamineStatusService.save(examineStatus);
	 	}
     
	 	//查询此证书的资质项
	 	Map<String, Object> aptitudeExtParams = new HashMap<String, Object>();
	    	aptitudeExtParams.put("aptitudeId", sysOrgAptitudeEntity.getId());//
	    List<SysAptitudeExtEntity> sysAptitudeExtList = sysAptitudeExtService.queryAll(aptitudeExtParams);
	   //把此证书资质项状态置为通过
	    if(sysAptitudeExtList !=null && sysAptitudeExtList.size()>0){
	    	for(SysAptitudeExtEntity sysAptitudeExtEntity:sysAptitudeExtList){
		    	sysAptitudeExtEntity.setAptitudeExtStatus(AptitudeEnum.aptitudeStatus.YBSYPZ.getKey());
		    	sysAptitudeExtService.updateById(sysAptitudeExtEntity);
		    }
	    }
	    //把资质状态置为通过
	 	if(!AptitudeEnum.aptitudeStatus.YBSYPZ.getKey().equals(sysOrgAptitudeEntity.getAptitudeStatus())){
	 		sysOrgAptitudeEntity.setAptitudeStatus(AptitudeEnum.aptitudeStatus.YBSYPZ.getKey());
		      sysOrgAptitudeService.updateById(sysOrgAptitudeEntity);	
	 	}
 	
 	    //查询此单位其他资质证书
	 	Map<String, Object> otherParams = new HashMap<String, Object>();
		    	otherParams.put("other", sysOrgAptitudeEntity.getId());
		    	otherParams.put("orgNo", sysOrgAptitudeEntity.getOrgNo());
	 	List<SysOrgAptitudeEntity> otherList = sysOrgAptitudeService.queryAll(otherParams);
	 	//将此单位其他资质证书置为失效状态
	 	if(otherList != null && otherList.size()>0){
		 	for(SysOrgAptitudeEntity sysOrgAptitudeEntityPam: otherList){
	 				sysOrgAptitudeEntityPam.setAptitudeStatus("4");
	 				sysOrgAptitudeService.updateById(sysOrgAptitudeEntityPam);
	 			//查询此单位其他资质项(不再审核资质项)
//		    	Map<String, Object> otherAptitudeExtParams = new HashMap<String, Object>();
//		    	otherAptitudeExtParams.put("aptitudeId", sysOrgAptitudeEntityPam.getId());
//			    List<SysAptitudeExtEntity> otherAptitudeExtList = sysAptitudeExtService.queryAll(otherAptitudeExtParams);
			  //将此单位其他资质项置为失效状态
//			    if(otherAptitudeExtList !=null && otherAptitudeExtList.size()>0){
//			    	for(SysAptitudeExtEntity sysAptitudeExtEntity:otherAptitudeExtList){
//				    	sysAptitudeExtEntity.setAptitudeExtStatus("4");
//				    	sysAptitudeExtService.updateById(sysAptitudeExtEntity);
//				    }
//			    }
		 	}
	 	}

	return "";
 }
	
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public String noPassOrgAptitude(SysOrgAptitudeEntity sysOrgAptitudeEntity,String auditTxt,String auditor,String orgNo) {
		
		//审核状态
	   	 SysExamineStatusEntity examineStatus = new SysExamineStatusEntity();
	   		Map<String, Object> params = new HashMap<String, Object>();
	   		params.put("auditId", sysOrgAptitudeEntity.getId());
//	   		params.put("auditOrgNo", orgNo);
	   		params.put("auditType", AptitudeEnum.aptitudeType.APTITUDE.getKey());
	   		//审核记录
	   	List<SysExamineStatusEntity> list = sysExamineStatusService.queryAll(params);
	   	if(list != null && list.size()>0){
	   		    examineStatus.setId(list.get(0).getId());
	   		    examineStatus.setAuditTxt(auditTxt);
	   		    examineStatus.setAuditor(orgNo);
	   		 examineStatus.setAuditOrgNo(orgNo);
	   		    examineStatus.setAuditStatus(AptitudeEnum.aptitudeStatus.YBSBPZ.getKey());
	   		    sysExamineStatusService.updateById(examineStatus);
	   	}else{
	   			examineStatus.setAuditTxt(auditTxt);
	   			examineStatus.setAuditor(auditor);
	   			examineStatus.setAuditOrgNo(orgNo);
	   			examineStatus.setAuditTime(new Date());
	   			examineStatus.setAuditId(sysOrgAptitudeEntity.getId());
	   			examineStatus.setAuditType(AptitudeEnum.aptitudeType.APTITUDE.getKey());
	   			examineStatus.setAuditStatus(AptitudeEnum.aptitudeStatus.YBSBPZ.getKey());
	   	        sysExamineStatusService.save(examineStatus);
	   	}
	
	   	//将此证书下的资质项全部置为不通过(不再审核资质项)
//	   	Map<String, Object> aptitudeExtParams = new HashMap<String, Object>();
//	   	aptitudeExtParams.put("aptitudeId", sysOrgAptitudeEntity.getId());
//		    List<SysAptitudeExtEntity> sysAptitudeExtList = sysAptitudeExtService.queryAll(aptitudeExtParams);
//		    if(sysAptitudeExtList != null && sysAptitudeExtList.size()>0){
//		    	for(SysAptitudeExtEntity sysAptitudeExtEntity:sysAptitudeExtList){
//			    	sysAptitudeExtEntity.setAptitudeExtStatus("3"); //审核未通过
//			    	sysAptitudeExtService.updateById(sysAptitudeExtEntity);
//			    }
//		    }
		    
	   		sysOrgAptitudeEntity.setAptitudeStatus("3");
		    sysOrgAptitudeService.updateById(sysOrgAptitudeEntity);	

	return "SUCCESS";
 }	
		

	
	
}
